//
//  OptionViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 13/05/13.
//
//
#import <CoreData/CoreData.h>
#import "OptionViewController.h"
#import "Define.h"
#import "ComunicationsUtils.h"
#import "CoreDataManager.h"
#import "SettingUtils.h"
#import "UtenteCD.h"

@interface OptionViewController ()

@end

@implementation OptionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"update_interval"] != nil){
        NSInteger i = [[[NSUserDefaults standardUserDefaults] objectForKey:@"update_interval"] integerValue] ;
        self.slider.value = i;
        self.sliderValue.text = [NSString stringWithFormat:@"%d sec",(int)i];
    }else{
        self.sliderValue.text = [NSString stringWithFormat:@"%d sec",(int)30];
        
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:30] forKey:@"update_interval"];
    }
    
    [self.slider addTarget:self action:@selector(sliderValueChanged:)
          forControlEvents:UIControlEventValueChanged];
}

-(IBAction)sliderValueChanged:(UISlider *)sender
{
    NSLog(@"slider value = %f", sender.value);
    self.sliderValue.text = [NSString stringWithFormat:@"%d sec",(int)sender.value];
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sender.value] forKey:@"update_interval"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction) indietro{
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.375];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
}



-(IBAction) reset{
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"ATTENZIONE"
                                                          message: @"Il ripristino del sistema eliminerà ogni dato presente nel dispositivo, compresi tutti i dati in attesa di invio.\nRipristinare?"
                                                         delegate:self cancelButtonTitle:@"Ripristina" otherButtonTitles:@"Annulla", nil];
    [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
    [myAlertView show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        CoreDataManager *core = [[CoreDataManager alloc] initWhidUserLogged:nil];
        NSArray *utenti = [core getUtentiAll];
        for(UtenteCD *u in utenti){
            [SettingUtils set:SAVE_REVISION_USERS obj:@0 user:[u.idutente stringValue]];
            [SettingUtils set:SAVE_REVISION_INTERVENTS obj:@0 user:[u.idutente stringValue]];
            [SettingUtils set:SAVE_REVISION_CLIENTS obj:@0 user:[u.idutente stringValue]];
            [SettingUtils set:BUFFER_INTERVENTS obj:@0 user:[u.idutente stringValue]];
        }
        [core dropDatabase];
        [self indietro];
    }
}



@end
